<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: whos_online.php 366 2013-12-16 12:03:48Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 366 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-12-16 12:03:48 +0000 (Mon, 16 Dec 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

function check_cart($which) {
	global $db;

	if (STORE_SESSIONS == 'mysql')
		$session_data = $db->db_query("SELECT sesskey, value FROM ".TABLE_SESSIONS." WHERE sesskey = '".$which."'");
	else{
		if((file_exists(session_save_path().'/sess_'.$which)) && (filesize(session_save_path().'/sess_'.$which) > 0) ){
			$session_data = file(session_save_path().'/sess_'.$which);
			$session_data = trim(implode('', $session_data));
		}
	}
	$which_query = $session_data;

	$who_query = $db->db_query("SELECT
									session_id, 
									time_entry, 
									time_last_click
								FROM 
									".TABLE_WHOS_ONLINE."
								WHERE 
									session_id='".$which."'");

	$xx_mins_ago_long = (time() - 180);

	if (strstr($which_query->fields['value'],'"qty";i:0;')){
		if ($who_query->fields['time_last_click'] <= $xx_mins_ago_long)
			return 'inactive_nocart';
		else
			return 'active_nocart';
	}
	if (!strstr($which_query->fields['value'],'"qty";i:0;')){
		if ($who_query->fields['time_last_click'] < $xx_mins_ago_long)
			return 'inactive_cart';
		else
			return 'active_cart';
	}
}

$xx_mins_ago = (time() - 900);

$db->db_query("DELETE FROM ".TABLE_WHOS_ONLINE." WHERE time_last_click < '".$xx_mins_ago."'");
  
require(DIR_WS_INCLUDES.'metatag.php');

$menu_link = getMenuID('whos_online');
if(isset($_GET['reload']))
	echo '<meta http-equiv="refresh" content="'.$_GET['reload'].';url=whos_online.php?reload='.$_GET['reload'].'&'.$menu_link.'" />';
?>
<style type="text/css">
	.inactive_nocart{background: #FFB2B2;width: 16px; height: 16px;}
	.active_nocart{background: #B2CCFF;width: 16px; height: 16px;}
	.inactive_cart{background: #FFFFCC;width: 16px; height: 16px;}
	.active_cart{background: #E5FF99;width: 16px; height: 16px;}
	.no_session_bot{background: #FFF; border: 1px solid #CCC;width: 15px; height: 15px;}
</style>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellpadding="0" cellspacing="0">
  <tr>
    <td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
		<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
	</td>
    <td class="boxCenter" valign="top">
    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td>
        	<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
		    <tr>
		     <td class="pageHeading" width="60%">
		        <?php echo HEADING_TITLE; ?>
		     </td>
		   </tr>
		   </table>
        </td>
      </tr>
      <tr>
      	<td align="right">
      		<a class="button" href="whos_online.php?<?php echo $menu_link ?>">aus</a>
      		<a class="button" href="whos_online.php?reload=30&<?php echo $menu_link ?>">30 Sek.</a>
      		<a class="button" href="whos_online.php?reload=60&<?php echo $menu_link ?>">60 Sek.</a>
      		<a class="button" href="whos_online.php?reload=120&<?php echo $menu_link ?>">120 Sek.</a>
			<a class="button" href="whos_online.php?reload=180&<?php echo $menu_link ?>">180 Sek.</a>
      	</td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table width="100%" cellspacing="0" cellpadding="0" class="dataTable">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent">&nbsp;</td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_ONLINE; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_FULL_NAME; ?></td>
                <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_IP_ADDRESS; ?></td>
                <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_COUNTRY; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_ENTRY_TIME; ?></td>
                <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_LAST_CLICK; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_HTTP_REFERER; ?>&nbsp;</td>
                <td class="dataTableHeadingContent last"><?php echo TABLE_HEADING_USER_AGENT; ?></td>
              </tr>
				<?php
				  $whos_online = $db->db_query("SELECT DISTINCT 
					  								ip_address, 
					  								customer_id, 
					  								full_name, 
					  								time_entry, 
					  								time_last_click, 
					  								last_page_url, 
					  								http_referer, 
					  								session_id, 
					  								user_agent 
				  								FROM 
				  									".TABLE_WHOS_ONLINE." 
				  								ORDER BY 
				  									full_name, 
				  									ip_address");
				  $total_bots=0;
				  $total_me=0;
				  $td = 1;
				  $i = 0;
				  
				  while (!$whos_online->EOF) {
				  	if($td == 1) 
				  		$td_zelle = ' dunkel'; 
				  	else 
				  		$td_zelle='';
				    	
				  	$time_online = ($whos_online->fields['time_last_click'] - $whos_online->fields['time_entry']);
            $whos_online->fields['last_page_url'] = htmlentities($whos_online->fields['last_page_url']);

				  	if ((!isset($_GET['info']) || (isset($_GET['info']) && ($_GET['info'] == $whos_online->fields['session_id']))) && !isset($info)) 
				      $info = $whos_online->fields['session_id'];
				    
				    if (($whos_online->fields['session_id'] == '') | (!(isset($whos_online->fields['session_id']))))
				            $noSessionDetected = '&nbsp;?&nbsp;';
				    else
				            $noSessionDetected = '';
				    

					if ($i % 2 == 0)
			    		$f = 'dataTableRow';
			    	else
			    		$f = '';
			    					    	
			    	if ( $noSessionDetected == '' ) {$e .= ' '.check_cart($whos_online->fields['session_id']);}
			    		else {$e .= ' no_session_bot'; }
			    		
			    	if ($old_array['ip_address'] == $whos_online->fields['ip_address']) 
				    	$i++;
			    ?>
					<tr class="<?php echo $f; ?>">
		              <td class="dataTableContent" align="center"  valign="top">
						<?php echo '<span class="'.$e.'" style="display: inline-block;">&nbsp;</span>'; ?>
		              </td>
		                <td class="dataTableContent" valign="top"><?php echo gmdate('H:i:s', $time_online); ?></td>
		                <?php if ( $whos_online->fields['customer_id'] == 0 ) { ?> <!-- Guest -->
		                <td class="dataTableContent" valign="top"><?php echo $whos_online->fields['full_name']; ?></td>
		                <?php } else { ?>
		                <td class="dataTableContent" valign="top">
							<a href="customers.php?selected_box=customers&cID=<?php echo $whos_online->fields['customer_id']; ?>&action=edit">
								<?php echo $whos_online->fields['full_name']; ?>
							</a>
						</td>
		                <?php }
		               if ($whos_online->fields['ip_address'] == $_SERVER["REMOTE_ADDR"]){
		                 echo '<td class="dataTableContent" align="center">Ich!</td>';
		                 $total_me++;
		               }else{		
						switch (true)
						{
							case (stripos( $whos_online->fields['user_agent'], 'Googlebot-Image' ) !== false):
								$bot = 'Google Image';
								$bot_img = 'bot_google.png'; // 24 x 24
								$total_bots++;
								break;
							
							case (stripos( $whos_online->fields['user_agent'], 'Googlebot' ) !== false):
								$bot = 'Google';
								$bot_img = 'bot_google.png'; // 24 x 24
								$total_bots++;
								break;
							
							case (stripos( $whos_online->fields['user_agent'], 'Sogou' ) !== false):
								$bot = 'Sogou';
								$bot_img = 'bot_sogou.png'; // 24 x 24
								$total_bots++;
								break;
							
							case (stripos( $whos_online->fields['user_agent'], 'Baidu' ) !== false):
								$bot = 'Baidu';
								$bot_img = 'bot_baidu.png'; // 24 x 24
								$total_bots++;
								break;
							
							case (stripos($whos_online->fields['user_agent'], 'Yandex' ) !== false):
								$bot = 'Yandex';
								$bot_img = 'bot_yandex.png'; // 24 x 24
								$total_bots++;
								break;					
							
							case (stripos( $whos_online->fields['user_agent'], 'Yahoo! DE Slurp' ) !== false):
							case (stripos( $whos_online->fields['user_agent'], 'Yahoo! Slurp' ) !== false):
								$bot = 'Yahoo';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'bingbot' ) !== false):
								$bot = 'Bing';
								$bot_img = 'bot_bing.png';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'MJ12bot' ) !== false):
								$bot = 'Majestic12';
								$total_bots++;
								break;
							
							case (stripos( $whos_online->fields['user_agent'], 'Ezooms' ) !== false):
								$bot = 'Ezooms';
								$bot_img = 'bot_ezooms.png';
								$total_bots++;
								break;
							
							case (stripos( $whos_online->fields['user_agent'], 'facebookexternalhit' ) !== false):
								$bot = 'Facebook';
								#$bot_img = 'bot_ezooms.png';
								$total_bots++;
								break;
													
							case (stripos( $whos_online->fields['user_agent'], 'Infoseek SideWinder' ) !== false):
								$bot = 'Infoseek';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'AltaVista' ) !== false):
							case (stripos( $whos_online->fields['user_agent'], 'Scooter' ) !== false):
								$bot = 'AltaVista';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'ArchitextSpider' ) !== false):
								$bot = 'Excite';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'PaperLiBot' ) !== false):
								$bot = 'PaperLiBot';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'Twitter' ) !== false):
								$bot = 'Twitter';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'Summify' ) !== false):
								$bot = 'Summify';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'ia_archiver' ) !== false):
								$bot = 'Alexa';
								$total_bots++;
								break;
						
							case (stripos( $whos_online->fields['user_agent'], 'AbachoBOT' ) !== false):
							case (stripos( $whos_online->fields['user_agent'], 'Crawler V 0.2.x' ) !== false):
								$bot = 'Abacho';
								$total_bots++;
								break;

							default:
								$user = $whos_online->fields['ip_address'];
								$bot = '';
								break;
						}
						if(!empty($bot)) 
							echo '<td class="dataTableContent" align="center" valign="top">'.(!empty($bot_img) ? '<img src="images/icons/'.$bot_img.'" alt="'.$bot.'" title="'.$bot.'" />' : $bot).'</td>';
						else
							echo '<td class="dataTableContent" align="center" valign="top"><a href="http://www.db.ripe.net/whois?form_type=simple&full_query_string=&searchtext='.  $whos_online->fields['ip_address']. ' " target="_blank">'.$whos_online->fields['ip_address'].'</a></td>';
		               }
		               
		               $get_country = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 6, 2);
		               if(!$get_country)
		               		$country = 'unbekannt';
		               else{
		               		$title= $db->db_query("SELECT countries_name FROM ".TABLE_COUNTRIES." WHERE countries_iso_code_2 = '".$get_country."'");
		               		$country = '<img src="'.DIR_WS_IMAGES.'flaggen/'.$get_country.'.gif" title="'.$title->fields['countries_name'].'">';
		             
		               }
		               
		               ?>
		                <td class="dataTableContent" align="center" valign="top"><nobr><?php echo $country; ?>&nbsp;</nobr></TD>
		                <td class="dataTableContent" align="center" valign="top"><?php echo date('H:i:s', $whos_online->fields['time_last_click']); ?></td>
		                <td class="dataTableContent" valign="top">
		                	<a href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER).$whos_online->fields['last_page_url']; ?>" target="_blank">
		                	<?php if (preg_match('/^(.*)'.session_name().'=[a-f,0-9]+[&]*(.*)/i', $whos_online->fields['last_page_url'], $array)) { 
		                		echo $array[1].$array[2]; } else { echo $whos_online->fields['last_page_url']; }
		                	?></a>
						</td>
						<td class="dataTableContent" valign="top">
							<?php 
							if ($whos_online->fields['http_referer'] !='') {
								if(strlen($whos_online->fields['http_referer']) > 100)
									echo '<a target="_blank" href="'.$whos_online->fields['http_referer'].'">'.substr($whos_online->fields['http_referer'], 0, 100).'...</a>';
								else
									echo '<a target="_blank" href="'.$whos_online->fields['http_referer'].'">'.$whos_online->fields['http_referer'].'</a>'; 
							}else 
								echo '&nbsp;'; ?>
						</td>
						<td class="dataTableContent last" valign="top">
							<?php echo $whos_online->fields['user_agent']; ?>
						</td>
		              </tr>

				<?php
					if($td == 2) $td=1; else $td++;
					
				 $old_array = $whos_online->fields;
				 $whos_online->MoveNext();
				  }?>
		              <tr>
		              	<td colspan="9">
			              	<?php 
			              	echo '<span style="margin: 3px;width: 16px; height: 16px; background: #E5FF99; display: inline-block;">&nbsp;</span> '.TEXT_STATUS_ACTIVE_CART.
					            '<span style="margin: 8px;width: 16px; height: 16px; background: #FFFFCC; display: inline-block;">&nbsp;</span> '.TEXT_STATUS_INACTIVE_CART.
					            '<span style="margin: 8px;width: 14px; height: 14px; background: #FFF; display: inline-block; border: 1px solid #ccc">&nbsp;</span> '.TEXT_STATUS_NO_SESSION_BOT.
					            '<span style="margin: 8px;width: 16px; height: 16px; background: #B2CCFF; display: inline-block;">&nbsp;</span> '.TEXT_STATUS_ACTIVE_NOCART.
					            '<span style="margin: 8px;width: 16px; height: 16px; background: #FFB2B2; display: inline-block;">&nbsp;</span> '.TEXT_STATUS_INACTIVE_NOCART; 
			            	?>
		              	</td>
		              </tr>	<?php 
				  if (!$i) {
				        $i=0;
				  }
				  $total_dupes = $i;
				  $total_sess = $whos_online->_numOfRows;
				  $total_cust = $total_sess - $total_dupes - $total_bots - 1;
				?>
				<?php
				  if(isset($http_referer_url))
				  {
				?>
				  <tr>
				        <td class="smallText" colspan="8"><?php echo '<strong>'.TEXT_HTTP_REFERER_URL.':</strong> <a href="'.$http_referer_url.'">'.$http_referer_url.'</a>'; ?></td>
				  </tr>
				  <?php
				  }
				?>
            </table>
                       
		     	
		    
            <br />
            <?php
            	echo sprintf(TEXT_NUMBER_OF_CUSTOMERS, $total_sess);
            	echo '<br />Doppelte IPs:'. $total_dupes.'
            			<br/>Bots: '.$total_bots.'
            			<br>Reale Kunden: '.($total_cust > 0 ? $total_cust : '0');?>
            </td>

          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>